<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/4
 * Time: 15:49
 */

namespace app\admin\controller;


use think\Request;

class Admin extends common
{
    public function adminList(){
        $adminList = db('admin')->select();
        $this->assign('adminList',$adminList);
        return $this->fetch('adminlist');
    }

    /**
     * 账号停用开启
     * @return mixed
     */
    public function adminStop(){
        $id = input('get.id/d');
        $state= input('get.state/d');
        if($state == 1){
            if(db('admin')->where('id',$id)->update(['state'=>0])){
                return $this->adminList();
            }else{
                $this->error('出了点小问题,停用失败了!','Admin/adminList');
            }
        }elseif ($state == 0){
            if(db('admin')->where('id',$id)->update(['state'=>1])){
                return $this->adminList();
            }else{
                $this->error('出了点小问题,开启失败了!','Admin/adminList');
            }
        }
    }

    /**
     * 密码修改
     * @return mixed|string
     */
    public function adminEdit(){
        if(Request::instance()->isPost()){
            $name = input('post.name');
            $ypass = input('post.ypass');
            $xpass = input('post.xpass');
            if(trim($ypass) != '' && trim($xpass) != ''){
                $ypass = md5($ypass);
                $pass = db('admin')->where('name',$name)->value('pass');
                if($pass== $ypass){
                    if(db('admin')->where('name',$name)->update(['pass'=>md5($xpass)])){
                        return json_encode(['msg'=>1,'data'=>'更新成功!']);
                    }else{
                        return json_encode(['msg'=>0,'data'=>'更新失败!']);
                    }
                }else{
                    return json_encode(['msg'=>0,'data'=>'原密码错误!']);
                }
            }else{
                return json_encode(['msg'=>0,'data'=>'提交数据为空!']);
            }
        }else{
            $id = input('get.id/d');
            $data =  db('admin')->where('id',$id)->find();
            $this->assign('data',$data);
            return $this->fetch();
        }
    }

    public function adminAdd(){
        if(Request::instance()->isPost()){
            $name = input('post.name');
            $pass = input('post.pass');
            $repass = input('post.repass');
            $lv= input('post.lv');
            if(trim($name) != '' && trim($pass) != '' && trim($lv) != ''){
                if($pass == $repass){
                    $bool= db('admin')->where('name',$name)->find();
                    if($bool){
                        return json_encode(['msg'=>0,'data'=>'用户名被占用!']);
                    }
                    if(db('admin')->insert(['name'=>$name,'pass'=>md5($pass),'lv'=>$lv])){
                        return json_encode(['msg'=>1,'data'=>'添加成功,刷新页面显示!']);
                    }else{
                        return json_encode(['msg'=>0,'data'=>'添加失败!']);
                    }
                }else{
                    return json_encode(['msg'=>0,'data'=>'两次密码不同!']);
                }
            }else{
                return json_encode(['msg'=>0,'data'=>'数据为空!']);
            }
        }else{
            return $this->fetch();
        }

    }



}


